package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.socialMarketing.SocialListVO;
import com.burgeon.backend.SHOPBACKEND.entity.socialMarketing.SocialMarketingVO;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendSocialMarketingMapper;
import com.burgeon.backend.SHOPBACKEND.rest.QuerySocialMarketingChengjiaoTop1;
import com.burgeon.backend.SHOPBACKEND.service.BackendSocialMarketingService;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.StoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class BackendSocialMarketingServiceImpl implements BackendSocialMarketingService {

    @Autowired
    private BackendSocialMarketingMapper backendSocialMarketingMapper;
    @Autowired
    private BackendGoodsMapper backendGoodsMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private AccountMapper accountMapper;

    /**
     *
     * @param startTime 可不传 8位
     * @param endTime 可不传 8位
     * @param start 20
     * @param field A 员工 B店仓 C无条件
     * @param value
     * @param account
     * @return
     */
    @Override
    public List<SocialListVO> socialListData(String startTime, String endTime, Integer start, String field, String value, String account) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","empname");
        map.put("B","storename");
        map.put("C",null);


        List<SocialListVO> data = backendSocialMarketingMapper
                .socialListData(startTime, endTime, start * 20, map.get(field), value, account);

        Integer y = backendSocialMarketingMapper.socialListDataCount(startTime, endTime, map.get(field), value, account);
        for (SocialListVO socialListVO : data){
            if (null != socialListVO.getGid()){
                socialListVO.setY(y);
                socialListVO.setGoods(backendGoodsMapper.getGoodsById(socialListVO.getGid()));
            }
        }



        return data;
    }

    @Override
    public HashMap configData(String account) {
        return backendSocialMarketingMapper.configData(account);
    }

    @Override
    public void upShareTaskConfig(Integer num, String use, String tokne) {
        backendSocialMarketingMapper.upShareTaskConfig(use,num,tokne);
    }

    /**
     *
     * @param time
     * @param start
     * @param field A 导购名 B店名 C无条件 D是否完成
     * @param value
     * @param token
     * @return
     */
    @Override
    public List<SocialMarketingVO> getShareTaskMonitoring(String finish, String time, Integer start, String field, String value, String token) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","empname");
        map.put("B","storename");
        map.put("C",null);


        List<SocialMarketingVO> data = backendSocialMarketingMapper.getShareTaskMonitoring(finish,start*20, time, map.get(field), value, token);
        Integer y = backendSocialMarketingMapper.getShareTaskMonitoringCount(finish, time, map.get(field), value, token);
        for (SocialMarketingVO s:data){

            SocialMarketingVO name = backendSocialMarketingMapper.getShareTaskByEmpid(s.getEmpid(), token);
            s.setY(y);
            if (name!=null){
                s.setEmpname(name.getEmpname());
                s.setStorename(name.getStorename());
            }
        }

        return data;
    }

    @Override
    public HashMap headData(String token) {

        HashMap data = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String time = sdf.format(new Date());

        String fenxiang = backendSocialMarketingMapper.fenxiangshuTOP1(time,token);
        data.put("fenxiang",fenxiang);
        Info info = GetInfo(token);

        QuerySocialMarketingChengjiaoTop1 queryChengjiao = new QuerySocialMarketingChengjiaoTop1(info.getUrl(), info.getAccount(), info.getPassword());
        List<String> empIdList = backendSocialMarketingMapper.getEmpIdList(time, token);
        String[] PFIdArr = empIdList.toArray(new String[empIdList.size()]);

        try {
            data.put("chengjia",queryChengjiao.getData(PFIdArr));
        } catch (Exception e) {
            e.printStackTrace();
            data.put("chengjia","--");
        }

        String storeTOP1 = backendSocialMarketingMapper.storeTOP1(time,token);

        storeTOP1 = storeMapper.GetStoreNameByErpid(storeTOP1);
        data.put("store",storeTOP1);
        /*String timeTOP = backendSocialMarketingMapper.timeTOP(time,token);*/

        String timeTOP = backendSocialMarketingMapper.timeTOP(token);

        if(Integer.parseInt(timeTOP)>=0 && Integer.parseInt(timeTOP) < 8 && timeTOP != null && timeTOP !=""){
            timeTOP = "00:00 ~ 08:00";
            System.err.println(timeTOP);
        }else if(Integer.parseInt(timeTOP)>=8 && Integer.parseInt(timeTOP) < 10 && timeTOP != null && timeTOP !=""){
            timeTOP = "08:00 ~ 10:00";
            System.err.println(timeTOP);
        }else if(Integer.parseInt(timeTOP)>=10 && Integer.parseInt(timeTOP) < 12 && timeTOP != null && timeTOP !=""){
            timeTOP = "10:00 ~ 12:00";
            System.err.println(timeTOP);
        }else if(Integer.parseInt(timeTOP)>=12 && Integer.parseInt(timeTOP) < 14 && timeTOP != null && timeTOP !=""){
            timeTOP = "12:00 ~ 14:00";
            System.err.println(timeTOP);
        }else if(Integer.parseInt(timeTOP)>=14 && Integer.parseInt(timeTOP) < 16 && timeTOP != null && timeTOP !=""){
            timeTOP = "14:00 ~ 16:00";
            System.err.println(timeTOP);
        }else if(Integer.parseInt(timeTOP)>=16 && Integer.parseInt(timeTOP) < 18 && timeTOP != null && timeTOP !=""){
            timeTOP = "16:00 ~ 18:00";
            System.err.println(timeTOP);
        }else if(Integer.parseInt(timeTOP)>=18 && Integer.parseInt(timeTOP) < 20 && timeTOP != null && timeTOP !=""){
            timeTOP = "18:00 ~ 20:00";
            System.err.println(timeTOP);
        }else if(Integer.parseInt(timeTOP)>=20 && Integer.parseInt(timeTOP) < 22 && timeTOP != null && timeTOP !=""){
            timeTOP = "20:00 ~ 22:00";
            System.err.println(timeTOP);
        }else if(Integer.parseInt(timeTOP)>=22 && Integer.parseInt(timeTOP) <= 23 && timeTOP != null && timeTOP !=""){
            timeTOP = "22:00 ~ 00:00";
            System.err.println(timeTOP);
        }

        data.put("time",timeTOP);

        String goodsTOP = backendSocialMarketingMapper.goodsTOP(time,token);
        goodsTOP = backendGoodsMapper.getGoodsById(goodsTOP).getName();
        data.put("goods",goodsTOP);
        return data;
    }

    /**
     * 任务监控
     * @param token
     * @return
     */
    @Override
    public List<SocialMarketingVO> getShareTaskList(String time, Integer start, String field, String value, String token) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("A","empname");
        map.put("B","storename");

        return backendSocialMarketingMapper.getShareTaskList(start,map.get(field),value,token);
    }

    private Info GetInfo(String token) {
        return accountMapper.GetAccData(token);
    }
}
